package org.blade.personal.dao.sys.impl;

import java.util.List;

import javax.persistence.Query;

import org.blade.personal.dao.base.BaseDao2;
import org.blade.personal.dao.sys.UserDao;
import org.blade.personal.mode.sys.SystemUser;
import org.blade.personal.utils.Pager;
import org.springframework.stereotype.Repository;

@Repository("UserDao")
public class UserDaoImpl extends BaseDao2<SystemUser,Long> implements UserDao {

	@Override
	public void add(SystemUser u) {
		super.add(u);
	}

	@Override
	public void update(SystemUser u) {
		super.update(u);
		
	}

	@Override
	public int delete(final String ids) {
		final String deleteHql = " delete from sys_usr where id in(?) ";
		return this.entityManager
				.createNativeQuery(deleteHql)
				.setParameter(1, ids)
				.executeUpdate();
	}

	@Override
	public int delete(Long id) {
		return this.delete(id.toString());
	}

	@Override
	public List<SystemUser> queryForList(String querySql) {
		return this.findObjectListBySql(querySql, null);
	}

	@Override
	public SystemUser queryOne(Long id) {
		return this.getById(id);
	}

	@Override
	public SystemUser queryForOne(String querySql, Object[] params) {
		
		Query query = this.entityManager.createQuery(querySql);
		
		for(int i = 0; i< params.length; i++){
			query.setParameter(i+1, params[i]);
		}
		
		return (SystemUser)query.getSingleResult();
	}

	@Override
	public int getCount(String countSql) {
		Long count = (Long)this.entityManager.createNativeQuery(countSql).getSingleResult();
		return count.intValue();
		
	}
	
	@Override
	public List<SystemUser> getAllUser() {
		return this.findList();
	}

	@Override
	public void pagination(Pager<SystemUser> page, SystemUser user) {
		this.pagination(page, user);
		
	}

}
